<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Pubsubhubbub_Subscribe {
    
    // TODO, refactor this. It's jsut a proof of concept now
    public function subscribe($user, Discovery_Uri $discoverer, $title, Array $conf) {
        
        $loader = new Minime_Pubsubhubbub_Subscription_Loader($conf);
        $storer = new Minime_Pubsubhubbub_Subscription_Storer($conf);
        
        $array['subid'] = Minime_Pubsubhubbub_Subscription::calculateSubIdFromNode($discoverer->getAtomFeedUri());
        $ps = $loader->getSubscription($array['subid']);

        if(!$ps) { 
            $array['callback'] = Minime_HrefRenderer::getPubsubhubbubCallback($array['subid'], $conf);
            $array['topic'] = $discoverer->getAtomFeedUri();
            $array['lease_seconds'] = 2*60*60;
            $array['secret'] = Minime_Pubsubhubbub_Subscription::createSecret();
            $array['verify_token'] = Minime_Pubsubhubbub_Subscription::createVerifyToken($discoverer->getAtomFeedUri());
            $array['title'] = $title;
            $array['hub'] = $discoverer->getHubUri();       
     
            // Add hub to subscription
            $ps = Minime_Pubsubhubbub_Subscription::buildFromArray($array);
        
            $storer->stroreSubscription($ps->getSubID(), $ps->asXML());
        
            // subscribe
            if(!$this->subscribeToHub($ps)) {
                // TODO if failure, delete subscription, return false
                echo "\n\n\t\tERROR ERROR IN PUBSUBHUBBUB SUBSCRIPTION!";
                return false;
            }
        } 
        
        $ps->addSubscriber(new Minime_Pubsubhubbub_MinimeSubscriber($user));
        $storer->stroreSubscription($ps->getSubID(), $ps->asXML());
       
        return true;
    }
    
    private function execute() {
        
    }
    
    private function subscribeToHub(Minime_Pubsubhubbub_Subscription $ps) {
        //$hub = "http://identi.ca/main/push/hub";
	    $request = curl_init($ps->getHub());
	    curl_setopt($request, CURLOPT_POST, TRUE);
	    curl_setopt($request, CURLOPT_POSTFIELDS, $ps->asSubscribePostFields());
	    curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
	    echo "Reply: " . curl_exec($request);
	    $code = curl_getinfo($request, CURLINFO_HTTP_CODE);
	    $ret = true;
	    if (in_array($code, array(202, 204))) {
	      echo "Positive response to \"subscribe\" request ($code)";
	      //return true;
	    } else {
	      echo "Error issuing \"subscribe\" request to " . $ps->getHub() . " ($code).";
	      //:print_r($ps->asSubscribePostFields());
	      $ret = false;
	    }   
	    curl_close($request);
	    return $ret;    
    }
    
    public static function createID($topic) {
        return md5($topic);
    }
}
